﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using DevExpress.XtraEditors.Controls;
using Dogshow.Model;
using Dogshow.Win.DataAccess;
using Shell;
using System.Linq;
using Shell.Events;
using RasaPsa = Dogshow.Win.Models.RasaPsa;
using Dogshow.Win.Helpers;

namespace Dogshow.Win.Views
{
    public class SifarnikRasaPasa : ViewBase
    {
        private DevExpress.XtraGrid.GridControl gridControl1;
        private System.Windows.Forms.BindingSource rasaPsaBindingSource;
        private System.ComponentModel.IContainer components;
        private DevExpress.XtraGrid.Views.Grid.GridView gridView1;

        IDataService m_DataService = new WebDataService();
        private DevExpress.XtraEditors.DXErrorProvider.DXErrorProvider dxErrorProvider1;
        private DevExpress.XtraGrid.Columns.GridColumn colNaziv;
        private DevExpress.XtraGrid.Columns.GridColumn colFciGrupa;
        private DevExpress.XtraEditors.Repository.RepositoryItemImageComboBox repositoryItemImageComboBox1;
        private System.Windows.Forms.ImageList imageList1;
        BindingList<Models.RasaPsa> m_RasePasa = new BindingList<Models.RasaPsa>();
    
        public SifarnikRasaPasa()
        {
            InitializeComponent();
        }

        protected override void OnLoad(System.EventArgs e)
        {
            OnRefresh();
        }

        public override void OnNew()
        {
            m_RasePasa.Add(new RasaPsa());
        }

        public override void OnRefresh()
        {
            var fciGrupe = m_DataService.VratiFciGrupe();
            repositoryItemImageComboBox1.Items.AddRange(fciGrupe.Select(x => new ImageComboBoxItem(x.Naziv, x, x.Id -1)).ToArray());

            var rase = m_DataService.VratiRasePasa().Select(x => new RasaPsa(x)).ToList();
            rase.MarkUnchanged();
            m_RasePasa = new BindingList<RasaPsa>(rase);
            rasaPsaBindingSource.DataSource = m_RasePasa;
        }

        public override void OnSave()
        {
            gridView1.CloseEditor();
            if (m_RasePasa.Any(rasaPsa => !rasaPsa.IsValid))
            {
                OnAlertChanged(new AlertEventArgs("Ispravite sve greške pre snimanja"));
                return;
            }
            m_DataService.Sacuvaj(m_RasePasa.ToArray());
            OnRefresh();
            OnAlertChanged(new AlertEventArgs("Podaci su uspešno sačuvani"));
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SifarnikRasaPasa));
            this.gridControl1 = new DevExpress.XtraGrid.GridControl();
            this.rasaPsaBindingSource = new System.Windows.Forms.BindingSource(this.components);
            this.gridView1 = new DevExpress.XtraGrid.Views.Grid.GridView();
            this.colFciGrupa = new DevExpress.XtraGrid.Columns.GridColumn();
            this.repositoryItemImageComboBox1 = new DevExpress.XtraEditors.Repository.RepositoryItemImageComboBox();
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.colNaziv = new DevExpress.XtraGrid.Columns.GridColumn();
            this.dxErrorProvider1 = new DevExpress.XtraEditors.DXErrorProvider.DXErrorProvider(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.gridControl1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.rasaPsaBindingSource)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.gridView1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.repositoryItemImageComboBox1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dxErrorProvider1)).BeginInit();
            this.SuspendLayout();
            // 
            // gridControl1
            // 
            this.gridControl1.DataSource = this.rasaPsaBindingSource;
            this.gridControl1.Dock = System.Windows.Forms.DockStyle.Left;
            this.gridControl1.Location = new System.Drawing.Point(0, 0);
            this.gridControl1.MainView = this.gridView1;
            this.gridControl1.Name = "gridControl1";
            this.gridControl1.RepositoryItems.AddRange(new DevExpress.XtraEditors.Repository.RepositoryItem[] {
            this.repositoryItemImageComboBox1});
            this.gridControl1.Size = new System.Drawing.Size(403, 437);
            this.gridControl1.TabIndex = 0;
            this.gridControl1.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] {
            this.gridView1});
            // 
            // rasaPsaBindingSource
            // 
            this.rasaPsaBindingSource.DataSource = typeof(Dogshow.Win.Models.RasaPsa);
            // 
            // gridView1
            // 
            this.gridView1.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
            this.colFciGrupa,
            this.colNaziv});
            this.gridView1.GridControl = this.gridControl1;
            this.gridView1.GroupCount = 1;
            this.gridView1.GroupFormat = "[#image]{1}";
            this.gridView1.Name = "gridView1";
            this.gridView1.OptionsView.ShowFooter = true;
            this.gridView1.OptionsView.ShowGroupPanel = false;
            this.gridView1.RowHeight = 16;
            this.gridView1.SortInfo.AddRange(new DevExpress.XtraGrid.Columns.GridColumnSortInfo[] {
            new DevExpress.XtraGrid.Columns.GridColumnSortInfo(this.colFciGrupa, DevExpress.Data.ColumnSortOrder.Ascending)});
            // 
            // colFciGrupa
            // 
            this.colFciGrupa.ColumnEdit = this.repositoryItemImageComboBox1;
            this.colFciGrupa.FieldName = "FciGrupa";
            this.colFciGrupa.Name = "colFciGrupa";
            this.colFciGrupa.Visible = true;
            this.colFciGrupa.VisibleIndex = 0;
            // 
            // repositoryItemImageComboBox1
            // 
            this.repositoryItemImageComboBox1.AutoHeight = false;
            this.repositoryItemImageComboBox1.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
            this.repositoryItemImageComboBox1.LargeImages = this.imageList1;
            this.repositoryItemImageComboBox1.Name = "repositoryItemImageComboBox1";
            this.repositoryItemImageComboBox1.SmallImages = this.imageList1;
            // 
            // imageList1
            // 
            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
            this.imageList1.Images.SetKeyName(0, "Fci1.jpg");
            this.imageList1.Images.SetKeyName(1, "Fci2.jpg");
            this.imageList1.Images.SetKeyName(2, "Fci3.jpg");
            this.imageList1.Images.SetKeyName(3, "Fci4.jpg");
            this.imageList1.Images.SetKeyName(4, "Fci5.jpg");
            this.imageList1.Images.SetKeyName(5, "Fci6.gif");
            this.imageList1.Images.SetKeyName(6, "Fci7.gif");
            this.imageList1.Images.SetKeyName(7, "Fci8.jpg");
            // 
            // colNaziv
            // 
            this.colNaziv.Caption = "Naziv rase";
            this.colNaziv.FieldName = "NazivRase";
            this.colNaziv.Name = "colNaziv";
            this.colNaziv.OptionsColumn.ShowCaption = false;
            this.colNaziv.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] {
            new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Count, "NazivRase", "Ukupno: {0:n0}")});
            this.colNaziv.Visible = true;
            this.colNaziv.VisibleIndex = 0;
            this.colNaziv.Width = 292;
            // 
            // dxErrorProvider1
            // 
            this.dxErrorProvider1.ContainerControl = this;
            this.dxErrorProvider1.DataSource = this.rasaPsaBindingSource;
            // 
            // SifarnikRasaPasa
            // 
            this.ClientSize = new System.Drawing.Size(500, 437);
            this.Controls.Add(this.gridControl1);
            this.Name = "SifarnikRasaPasa";
            ((System.ComponentModel.ISupportInitialize)(this.gridControl1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.rasaPsaBindingSource)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.gridView1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.repositoryItemImageComboBox1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dxErrorProvider1)).EndInit();
            this.ResumeLayout(false);

        }
    }
}